﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

/// <summary>
/// StringUtil 的摘要说明
/// 字符串的处理，字符的转换
/// </summary>
public class StringUtil
{
	public StringUtil()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    /// <summary>
    /// 删除不可见字符
    /// </summary>
    /// <param name="sourceString"></param>
    /// <returns></returns>
    public static string DeleteUnVisibleChar(string sourceString)
    {
        System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(131);
        for (int i = 0; i < sourceString.Length; i++)
        {
            int Unicode = sourceString[i];
            if (Unicode >= 16)
            {
                sBuilder.Append(sourceString[i].ToString());
            }
        }
        return sBuilder.ToString();
    }

    public static string EmptyToZero(object obj)
    {
        if (obj == null)
        {
            return "0";
        }

        string str = obj.ToString().Trim();

        if (str.Equals(""))
        {
            return "0";
        }
        else
        {
            return str;
        }
    }

    public static bool IsEmpty(string str)
    {
        if (str == null || str.Trim().Equals(""))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static bool IsNotEmpty(string str)
    {
        return !StringUtil.IsEmpty(str);
    }

    public static string StringAddAsDecimal(string str1, string str2)
    {
        decimal a = decimal.Parse(StringUtil.EmptyToZero(str1));
        decimal b = decimal.Parse(StringUtil.EmptyToZero(str2));
        decimal c = decimal.Add(a, b);
        return c.ToString();        
    }

    public static string toString(decimal d)
    {
        if (d > 0)
        {
            return "+" + d.ToString();
        }
        else
        {
            return d.ToString();
        }
    }

    public static string addPlus(object o)
    {
        if (o == null)
        {
            return "";
        }

        string s = o.ToString();
        if (s == "")
        {
            return "";
        }

        double d = Convert.ToDouble(s);
        if (d >= 0)
        {
            return "+" + d.ToString("0.00");
        }
        else
        {
            return d.ToString("0.00");
        }
    }

    public static string safeText(string inputString)//过滤SQL输入字符串防止攻击
    {
        if ((inputString != null) && (inputString != String.Empty))
        {
            inputString = inputString.Trim();
            inputString = inputString.Replace("'", "''");
            inputString = inputString.Replace(";", "；");
            inputString = inputString.Replace("--", "—");
            return inputString.ToString();
        }
        return "";
    }


    public static ArrayList splitStringBySize(string s, int size)
    {
        ArrayList list = new ArrayList();

        do
        {
            if (s.Length <= size)
            {
                list.Add(s);
                break;
            }
            else
            {
                list.Add(s.Substring(0, size));
                s = s.Substring(size, s.Length - size);

            }
        } while (true);
        return list;
    }
}
